using System;

using Impact.Infrastructure.Common;

namespace Impact.Infrastructure.State
{
    /// <summary>
    /// a transaction is a single thread of request running through impact.  it carries with it
    /// pertinent information as well as a unique identifier.
    /// </summary>
    public  class Transaction
    {

        #region Static Accessors

        /// <summary>
        /// retrieve the transaction associated w/ the current executing thread
        /// </summary>
        public static Transaction Current
        {
            get
            {
                return ThreadLocalStorage.GetThreadObject<Transaction>(typeof(Transaction).ToString());
            }
        }

        /// <summary>
        /// assign a transaction object to the current executing thread
        /// </summary>
        /// <param name="transaction"></param>
        public static void Set(Transaction transaction)
        {
            ThreadLocalStorage.SetThreadObject(typeof(Transaction).ToString(), transaction);
        }

        #endregion



        private Guid transactionId;
        private string sessionId;
        private Guid? userId;

        /// <summary>
        /// default constructor
        /// </summary>
        public Transaction()
        {
            transactionId = Guid.NewGuid();
        }

        /// <summary>
        /// uniquely identifies this transaction
        /// </summary>
        public Guid TransactionId
        {
            get { return transactionId; }
        }

        /// <summary>
        /// uniquely identifies this transaction to a particular session
        /// </summary>
        public string SessionId
        {
            get { return sessionId; }
            set { sessionId = value; }
        }

        /// <summary>
        /// the ID of the user logged in, if any
        /// </summary>
        public Guid? UserId
        {
            get { return userId; }
            set { userId = value; }
        }


    }
}
